'''
Created on 2011-07-02

@author: Andrew Roth
'''
import csv

cdef class PositionCountIterator(object):
    '''
    Class for iterating over rows in a Counter object which match those
    specified by a tab delimited file of the form 
    
    ref position
    '''
    def __init__(self, Counter counter, PositionsIter positions_iter):
        self._counter = counter
        self._positions_iter = positions_iter
        
        self._counter_ref = "\0"
        self._current_ref = 'abcdefg'
        
    def __iter__(self):
        return self
    
    def __next__(self):
        self._advance_positions_iter()
        
        while True:
            if self._current_ref != self._counter_ref:
                self._set_new_counter_ref()
            
            if self._current_position == self._current_row._position:
                return self._current_row
            
            elif self._current_position > self._current_row._position:
                self._advance_current_row()
            
            elif self._current_position < self._current_row._position:
                self._advance_positions_iter()
    
    cdef _set_new_counter_ref(self):
        self._counter_ref = self._current_ref
        self._counter_iter = self._counter.iter_ref(self._counter_ref)
        
        self._counter_iter.cnext()
        self._current_row = self._counter_iter._current_row
        
    cdef _advance_current_row(self):
        '''
        Move to next row in counter. If we are at the end of the counter iterator
        for the ref move reader along to catch up.
        '''
        try:
            self._counter_iter.cnext()
            self._current_row = self._counter_iter._current_row
        except StopIteration:
            self._advance_positons_iter_to_next_ref()
            
    cdef _advance_positions_iter(self):
        while True:
#            ref, position = self._positions_iter.next()
            self._positions_iter.cnext()
            
            if self._positions_iter._ref not in self._counter.refs:
                continue
                      
            self._current_ref = self._positions_iter._ref
            self._current_position = self._positions_iter._position
            
            break
            
    cdef _advance_positons_iter_to_next_ref(self):
        '''
        Move the reader along until we find a new ref.
        '''
        while True:
            self._advance_positions_iter()
            
            if self._current_ref != self._counter_ref:
                break

cdef class PositionsIter(object):
    cdef cnext(self):
        raise NotImplemented

cdef class CharacterDelimitedPositionsReader(PositionsIter):
    '''
    Class for reading positions file of the form
    
    ref{delimiter_char}position
    '''
    def __init__(self, positions_file_name, delimiter_char='\t'):
        self._positions_file = csv.reader(open(positions_file_name), delimiter=delimiter_char)
    
    def __iter__(self):
        return self
    
    def __next__(self):
        pass

    cdef cnext(self):
        row = self._positions_file.next()
        
        self._ref = row[0]
        self._position = < int > (int(row[1]) - 1)
